package de.tub.bazinga.object;

import processing.core.PApplet;
import de.tub.bazinga.main.Bazinga;
import de.tub.bazinga.object.interf.Drawable;

public class Countdown implements Drawable {

	private static final long COUNTDOWN_START_TIME = 5000;
	private static final int TEXT_SIZE = 200;

	private long startTime = 0;
	private boolean wasStarted = false;

	// Callback um das Spiel zu starten wenn der Countdown abgelaufen ist.
	private Bazinga game = null;

	public Countdown(Bazinga game) {
		super();
		this.game = game;
	}

	@Override
	public void draw(PApplet window) {
		long timeDifference = System.currentTimeMillis() - startTime;
		if (timeDifference < COUNTDOWN_START_TIME) {
			Integer timeToDisplay = (int) ((COUNTDOWN_START_TIME - timeDifference) / 1000);

			int x = window.getWidth() / 2 - TEXT_SIZE / 2;
			int y = window.getHeight() / 2;

			window.fill(245);
			window.textSize(TEXT_SIZE);
			window.text(timeToDisplay, x, y);
		} else {
			if (wasStarted) {
				game.startGame();
				wasStarted = false;
			}
		}
	}

	public void start() {
		startTime = System.currentTimeMillis();
		wasStarted = true;
	}

	public void stop() {
		startTime = 0l;
		wasStarted = false;
	}

}
